package com.heba.popupwindow;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.heba.greenmulu.R;
import com.heba.popupwindow.callback.DiaryCommentListener;
import com.heba.utils.CommonUtils;
import com.heba.utils.DensityUtils;


/**
 * 日記評論彈出框
 * Created by shenghao on 2019/3/10.
 */
public class DiaryCommentPopupWindow extends BasePopupWindow implements View.OnClickListener {

    private EditText et_comment;
    private TextView tv_send;

    private DiaryCommentListener listener;

    private String content;  //評論內容

    public DiaryCommentPopupWindow(Context context) {
        super(LayoutInflater.from(context).inflate(
                R.layout.popup_comment, null));
//        setAnimationStyle(R.style.Popup_Animation_Translate);
    }

    public DiaryCommentPopupWindow(Context context, int width, int height) {
        super(context, LayoutInflater.from(context).inflate(
                R.layout.popup_comment, null), width, height);
//        setAnimationStyle(R.style.Popup_Animation_Translate);
    }

    @Override
    public void initViews() {
        et_comment = (EditText) findViewById(R.id.et_comment);
        tv_send = (TextView) findViewById(R.id.tv_send);
    }

    @Override
    public void initEvents() {
        tv_send.setOnClickListener(this);
    }

    @Override
    public void init() {
        tv_send.setEnabled(false);

        /**
         * 監聽評論輸入
         */
        et_comment.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    tv_send.setEnabled(true);
                    content = CommonUtils.removeBlankLine(s.toString());
                } else {
                    tv_send.setEnabled(false);
                }
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_send:
                if (listener != null) {
                    if (content.trim().isEmpty())
                        return;
                    listener.onSendClick(CommonUtils.getEncodeText(content));
                }
                break;
            default:
                break;
        }
        dismiss();
    }

    /**
     * 設置hint
     *
     * @param text
     */
    public void setCommentHint(String text) {
        et_comment.setHint(text);
    }

    /**
     * 設置內容
     *
     * @param text
     */
    public void setCommentText(String text) {
        et_comment.setText(text);
    }

    /**
     * 顯示軟鍵盤
     */
    public void showSoftKeyBoard() {
        DensityUtils.showSoftKeyBoard(context, et_comment);
    }

    /**
     * @param listener
     */
    public void setOnDiaryCommentListener(DiaryCommentListener listener) {
        this.listener = listener;
    }

}
